package com.woniu.user.controller;

import cn.hutool.jwt.JWT;
import cn.hutool.jwt.JWTUtil;
import com.alibaba.druid.util.StringUtils;
import com.woniu.common.constans.Constants;
import com.woniu.common.pojo.Shops;
import com.woniu.common.pojo.User;
import com.woniu.common.utils.TokenUtil;
import com.woniu.common.vo.AjaxResult;
import com.woniu.common.vo.ShopsDto;
import com.woniu.common.vo.UserDto;
import com.woniu.common.vo.Users;
import com.woniu.user.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {
    @Resource
    private UserService userService;

    //    登入
    @PostMapping("/login")
    public AjaxResult logins(@RequestBody Users user, HttpServletResponse response) {
        UserDto shopsDto = userService.logins(user);
//        return AjaxResult.success("登录成功", shopsDto);
        return AjaxResult.success("登录成功", shopsDto.getToken());
    }


    @RequestMapping("/add")
    public AjaxResult add(@RequestBody User user) {
        userService.insertUser(user);
        return AjaxResult.success("添加成功");
    }

    //    查名字进行重名判断
    @PostMapping("/selectUserName")
    public AjaxResult selectUserName(@RequestBody User user) {
        System.out.println("y要查询查询：" + user.getUsername());
        int i = userService.selectUserName(user.getUsername());
        if (i == 1) {
            return AjaxResult.success("该名字已存在，请重新输入。");
        }
        return AjaxResult.error("商家名称可用");
    }


    //   查询
    @RequestMapping("/selectOne")
    public AjaxResult selectOne(HttpServletRequest request) {
        User user = TokenUtil.getObj(request, User.class);
        User tUsers = userService.selectUser(user);
        System.out.println(tUsers + "--------------------");
        return AjaxResult.success("查询成功!", tUsers);
    }

    @RequestMapping("/update")
    public AjaxResult update(@RequestBody User user) {
        userService.update(user);
        return AjaxResult.success("修改成功!", user);
    }

    @RequestMapping("/getTook")
    public AjaxResult getTook(HttpServletRequest request) {
        User obj = TokenUtil.getObj(request, User.class);
        return AjaxResult.success("修改成功,请重新登录!", obj);
    }

    @RequestMapping(value = "/delete/{ids}", method = RequestMethod.POST)
    public AjaxResult delete(@PathVariable Integer ids) {
        userService.delete(ids);
        return AjaxResult.success("删除成功!");
    }
}

